package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblThongTinHocVientblThongTinHocVienMonHocInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public void setId(Long id) {
		this.id = id;
	}

	private int hocVienId;

	public void setHocVienId(int hocVienId) {
		this.hocVienId = hocVienId;
	}

	public int getHocVienId() {
		return hocVienId;
	}

	private TblThongTinHocVienService tblThongTinHocVienService;

	private Long monHocId;

	public void setMonHocId(Long monHocId) {
		this.monHocId = monHocId;
	}

	public Long getMonHocId() {
		return monHocId;
	}
	
	 private String maMonHoc;

	    public String getMaMonHoc() {
			return maMonHoc;
		}

		public void setMaMonHoc(String maMonHoc) {
			this.maMonHoc = maMonHoc;
		}

	private TblCTDTMonHocService tblCTDTMonHocService;

	private String trangThai;

	public void setTrangThai(String trangThai) {
		this.trangThai = trangThai;
	}

	public String getTrangThai() {
		return trangThai;
	}

	private TblThongTinHocVienMonHoc tblThongTinHocVienMonHoc;

	public TblThongTinHocVienMonHoc getTblThongTinHocVienMonHoc() {
		return tblThongTinHocVienMonHoc;
	}

	private List<TblThongTinHocVienMonHoc> tblThongTinHocVienMonHocs;

	public List<TblThongTinHocVienMonHoc> getTblThongTinHocVienMonHocs() {
		return tblThongTinHocVienMonHocs;
	}

	private TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService;

	private int preInsert(Message msg, TblThongTinHocVienMonHoc obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblThongTinHocVienMonHoc obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc",
					"them")) {
				return "noprivilege";
			}
			tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService) context
					.getBean("tbl_ThongTin_HocVien_MonHocClient");
			tblThongTinHocVienService = (TblThongTinHocVienService) context
					.getBean("tbl_ThongTin_HocVienClient");

			tblCTDTMonHocService = (TblCTDTMonHocService) context
					.getBean("tbl_CTDT_MonHocClient");

			msg = new Message();
			//-----------------------------------------
			//Tim kiem mon hoc voi ma mon hoc
			List<TblCTDTMonHoc> monhocs = findMonHocWithMa(maMonHoc,tblCTDTMonHocService);
			if (monhocs != null && monhocs.size() > 0) {
				monHocId = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			
			//==========================================
			
			tblThongTinHocVienMonHoc = new TblThongTinHocVienMonHoc();
			tblThongTinHocVienMonHoc.setHocVienId(tblThongTinHocVienService.gettblThongTinHocVien(new Long(hocVienId)));
			tblThongTinHocVienMonHoc.setMonHocId(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHocId)));
			tblThongTinHocVienMonHoc.setTrangThai(trangThai);
			
			preInsert(msg, tblThongTinHocVienMonHoc);
			if (msg.isPreMessage_b()) {
				tblThongTinHocVienMonHocService
						.createtblThongTinHocVienMonHoc(tblThongTinHocVienMonHoc);
				postInsert(msg, tblThongTinHocVienMonHoc);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.id");
			con.setValue1(new Long(hocVienId));
			con.setOperator("eq");
			cons.add(con);
						
			tblThongTinHocVienMonHocs = tblThongTinHocVienMonHocService.findtblThongTinHocVienMonHocByCondition(cons, pageSize,page);
			pages = tblThongTinHocVienMonHocService.counttblThongTinHocVienMonHocByCondition(cons)/ pageSize + 1;
			
			dataString = "";
			dataString += "&id=";
			dataString += "&hocVienId=";
			dataString += "&monHocId=";
			dataString += "&trangThai=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Insert tbl_ThongTin_HocVien_MonHoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
	
	private List<TblCTDTMonHoc> findMonHocWithMa(String ma, TblCTDTMonHocService tblCTDTMonHocService){
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(ma);
			con.setOperator("eq");
			cons.add(con);
		}

		List<TblCTDTMonHoc> monhocs = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 0, 0);
		
		return monhocs;
	}
}
